import 'package:fish_redux/fish_redux.dart';
import 'package:kunlun_gas/app_files.dart';
import 'action.dart';
import 'state.dart';

Effect<GasChargeViewState> buildEffect() {
  return combineEffects(<Object, Effect<GasChargeViewState>>{
    Lifecycle.initState: _initState,
    Lifecycle.dispose: _dispose,
    GasChargeViewAction.onSubmit: _onSubmit,
  });
}

void _dispose(Action action, Context<GasChargeViewState> ctx) {
  bus.off('bindSuccess');
}

void _initState(Action action, Context<GasChargeViewState> ctx) {
  bus.on('bindSuccess', (arg) {
    ctx.state.bluetoothNo = arg;
    ctx.dispatch(GasChargeViewActionCreator.onAction());
  });
}

void _onSubmit(Action action, Context<GasChargeViewState> ctx) async {
  if (ctx.state.moneyController.text.length == 0) {
    CommonUtils.showErrorInfo(ctx.context, '请输入支付金额');
    return;
  }
  CommonUtils.closeKey(ctx.context);
  CommonUtils.showWaitLoading(ctx.context);
  ResultData res = await CommonNetWork.submitPay(
      ctx.state.model.gasNo, ctx.state.moneyController.text);
  Navigator.pop(ctx.context);
  if (res.success) {
    CommonUtils.pushRoute(ctx.context, RoutePath.aliPayView, arguments: {
      'url': res.data['Url'],
      'orderNo': res.data['OrderNo'],
      'transNo': res.data['TransNo'],
    });
  } else {
    CommonUtils.showAlertView(ctx.context, res.desc);
  }
}
